/*
 * FileName: PrintThread.java
 * Author:   zzw
 * Date:     2018年04月18日
 * Description: 打印ABC10次
 */
package com.zzw.thread;

/**
 * 〈打印ABC10次〉<br>
 * 〈打印ABC10次〉
 *
 * @author zzw
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本]（可选）
 */
public class PrintThread implements Runnable{
    private Object object;
    private String content;
    private Object preObject;

    public PrintThread(String content, Object preObject,Object object) {
        this.object = object;
        this.content = content;
        this.preObject = preObject;
    }

    @Override
    public void run() {
        for (int i = 0;i < 1;i++){
            System.out.println("i:" + i);
            synchronized (preObject){
                synchronized (object){
                    System.out.print(content);
                    object.notify();
                    System.out.println("notify");
                }
                try {
                    System.out.println(Thread.currentThread().getName() + ":will wait");
                    preObject.wait();
                    System.out.println(Thread.currentThread().getName() + ":wait");
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();
        Thread threadA = new Thread(new PrintThread("A",c,a),"A");
        Thread threadB = new Thread(new PrintThread("B",a,b),"B");
        Thread threadC = new Thread(new PrintThread("C",b,c),"C");
        threadA.start();
        Thread.sleep(100);
        threadB.start();
        Thread.sleep(100);
        threadC.start();
    }
}
